package com.lz.sports.vm.config

import android.text.Editable
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lz.comm.SP
import com.lz.library.base.BaseModel
import com.lz.library.base.BaseViewModel
import com.lz.sports.R
import com.lz.sports.core.*
import com.lz.sports.utils.toArrayList

class ExamRuleViewModel : BaseViewModel<BaseModel>() {
    //模式
    val models by lazy { R.array.examRuleModel.toArrayList() }
    private val modelDes by lazy { R.array.examRuleModelDesc.toArrayList() }
    var examModelPosition = 0
    val examModel = MutableLiveData<String>()
    val examModelDes = MutableLiveData<String>()

    //缺考条件
    val conditions by lazy { R.array.examRuleCondition.toArrayList() }
    private val conditionDes by lazy { R.array.examRuleConditionDesc.toArrayList() }
    var examConditionPosition = 0
    val examCondition = MutableLiveData<String>()
    val examConditionDes = MutableLiveData<String>()

    //组数量
    val examGroupCount = MutableLiveData<String>()
    //考试次数
    val examCount = MutableLiveData<String>()

    //组与数量是否正常编辑
    private var groupCountEnabled = true
    private var examCountEnabled = true
    //保存按钮
    val saveEnabled = MutableLiveData(true)

    override fun onCreate(owner: LifecycleOwner) {
        examGroupCount.value = "${SP.getInt(K_RULE_EXAM_GROUP_COUNT, v_ruleExamGroupCount)}"
        examCount.value = "${SP.getInt(K_RULE_EXAM_COUNT, v_ruleExamCount)}"

        examModelPosition = SP.getInt(K_RULE_EXAM_Model, v_ruleExamModel)
        examModel.value = models.getOrNull(examModelPosition)
        examModelDes.value = modelDes.getOrNull(examModelPosition)

        examConditionPosition = SP.getInt(K_RULE_EXAM_CONDITION, v_ruleExamCondition)
        examCondition.value = conditions.getOrNull(examConditionPosition)
        examConditionDes.value = conditionDes.getOrNull(examConditionPosition)
    }

    fun selectExamModel(position: Int) {
        if (examModelPosition == position) return
        examModelPosition = position
        examModel.value = models.getOrNull(examModelPosition)
        examModelDes.value = modelDes.getOrNull(examModelPosition)
    }

    fun selectCondition(position: Int) {
        if (examConditionPosition == position) return
        examConditionPosition = position
        examCondition.value = conditions.getOrNull(examConditionPosition)
        examConditionDes.value = conditionDes.getOrNull(examConditionPosition)
    }

    fun afterGroupTextChanged(editable: Editable) {
        val groupCount = editable.toString().trim().toIntOrNull()
        groupCountEnabled = groupCount != null && groupCount > 0
        saveEnabled.value = groupCountEnabled && examCountEnabled
    }

    fun afterCountTextChanged(editable: Editable) {
        val examCount = editable.toString().trim().toIntOrNull()
        examCountEnabled = examCount != null
        saveEnabled.value = groupCountEnabled && examCountEnabled
    }

    fun save(isCheck: Boolean, isResit: Boolean, isTrip: Boolean, isSave: Boolean) {
        val groupCount = examGroupCount.value!!.trim().toInt()
        SP.setInt(K_RULE_EXAM_GROUP_COUNT, groupCount)
        val examCount = this.examCount.value!!.trim().toInt()
        SP.setInt(K_RULE_EXAM_COUNT, examCount)

        SP.setInt(K_RULE_EXAM_Model, examModelPosition)
        SP.setInt(K_RULE_EXAM_CONDITION, examConditionPosition)

        SP.setBoolean(K_RULE_EXAM_CHECK, isCheck)
        SP.setBoolean(K_RULE_EXAM_RESIT, isResit)
        SP.setBoolean(K_RULE_EXAM_TRIP, isTrip)
        SP.setBoolean(K_RULE_EXAM_SAVE, isSave)
    }

}